﻿using commonImportPlugin.Configs.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace commonImportPlugin.Configs.MatchFile
{
    public class MatchFileConfig : ConfigsBase,IcommonBase
    {
        //
        //    基本配置参数
        //
        #region 
        /// <summary>
        ///工厂配置
        /// </summary>
        public string factory { set; get; }
        /// <summary>
        /// 筛选数据文件规则
        /// (文件名正则[#]文件内容正则)
        /// </summary>
        public string fileFilterRules { set; get; }
        /// <summary>
        /// 编码格式
        /// </summary>
        public string encoding { set; get; }
        /// <summary>
        /// 匹配数据起始位置（1开始）
        /// </summary>
        public int dataIndex { set; get; }
        /// <summary>
        /// 匹配数据分隔符
        /// </summary>
        public string[] dataSeparators { set; get; }
        /// <summary>
        /// 获取匹配数据银行卡号数据规则
        /// </summary>
        public int[] subBankCardId { set; get; }
        /// <summary>
        /// 获取匹配数据身份证号规则
        /// </summary>
        public int[] subPersonId { set; get; }
        /// <summary>
        /// 社保卡号所处列
        /// </summary>
        public int[] subCardId { set; get; }
        ///// <summary>
        ///// 检查匹配数据格式的正则
        ///// </summary>
        //public string dataCheckRegex { set; get; }
        /// <summary>
        /// 该参数用于实现过滤之前已导入的数据
        /// 该参数值为：bankCardId,personId,ssCardId之一
        /// </summary>
        public string filterItem { set; get; }
        #endregion

        //
        //    其他
        //
        #region
        /// <summary>
        /// 单例
        /// </summary>
        private static MatchFileConfig uniqueInstance;

        /// <summary>
        /// 初始化配置
        /// </summary>
        /// <param name="configs"></param>
        public void Init()
        {         
            //检查参数配置的参数格式为：数字(+)&数字(-+)&数字(-+)
            string checkRegexs = @"^\d{1,}&(-\d{1,}|\d{1,})&(-\d{1,}|\d{1,})$";
        
            MatchFileConfig matchFileConfig = new MatchFileConfig();

            //工厂配置
            matchFileConfig.factory = configs["MatchFileFactory"];

            //文件扩展名
            if (!IsKeyExist("MatchFileFilterRules"))
                throw new Exception(MatchErrors.MatchFileFilterRulesError);

            matchFileConfig.fileFilterRules = configs["MatchFileFilterRules"];

            //编码方式
            if (!IsKeyEmpty("MatchFileEncoding"))
                matchFileConfig.encoding = configs["MatchFileEncoding"];
            else
                matchFileConfig.encoding = "GB2312";//默认值

            //数据起始行（1开始)
            if (!IsKeyExist("MatchFileDataIndex"))
                throw new Exception(MatchErrors.MatchFileDataIndexError1);

            if (!Regex.IsMatch(configs["MatchFileDataIndex"], @"\d+"))
                throw new Exception(MatchErrors.MatchFileDataIndexError2);

            matchFileConfig.dataIndex = int.Parse(configs["MatchFileDataIndex"]);

            //数据分隔符
            if (!IsKeyExist("MatchFileDataSeparators"))
                throw new Exception(MatchErrors.MatchFileDataSeparatorsError);

            matchFileConfig.dataSeparators = ToStringArrays(configs["MatchFileDataSeparators"]);

            //截取银行卡号
            if (!IsKeyEmpty("MatchFileSubBankCardId"))
            {
                if (!Regex.IsMatch(configs["MatchFileSubBankCardId"], checkRegexs))
                    throw new Exception(MatchErrors.MatchFileSubBankCardIdError);

                matchFileConfig.subBankCardId = ToIntArrays(configs["MatchFileSubBankCardId"]);
            }

            //截取身份证号
            if (!IsKeyEmpty("MatchFileSubPersonId"))
            {
                if (!Regex.IsMatch(configs["MatchFileSubPersonId"], checkRegexs))
                    throw new Exception(MatchErrors.MatchFileSubPersonIdError);

                matchFileConfig.subPersonId = ToIntArrays(configs["MatchFileSubPersonId"]);
            }

            //获取医保卡号
            if (!IsKeyEmpty("MatchFileSubCardId"))
            {
                if (!Regex.IsMatch(configs["MatchFileSubCardId"], checkRegexs))
                    throw new Exception(MatchErrors.MatchFileSubCardIdError);
                matchFileConfig.subCardId = ToIntArrays(configs["MatchFileSubCardId"]);
            }

            ////数据格式检查正则
            //if (IsKeyExist("MatchFileDataCheckRegex"))
            //{
            //    matchFileConfig.dataCheckRegex = configs["MatchFileDataCheckRegex"];
            //}

            //过滤数据项（1开始）
            if (!IsKeyEmpty("MatchFileFilterItem"))
            {
                string[] items = new string[] { "bankCardId", "personId", "ssCardId" };
                string item = configs["MatchFileFilterItem"].Trim();

                if (!items.Contains(item))
                    throw new Exception(MatchErrors.MatchFileFilterItemError);

                matchFileConfig.filterItem = configs["MatchFileFilterItem"];
            }

            uniqueInstance = matchFileConfig;
        }
        /// <summary>
        /// 获取配置
        /// </summary>
        /// <returns></returns>
        public static MatchFileConfig GetConfig()
        {
            if (uniqueInstance == null)
            {
                return null;
            }
            return uniqueInstance;
        }
        #endregion
    }
}
